package com.bizfx.component.views
{
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	
	import com.bizfx.component.mainform.IMainForm;
	import com.bizfx.events.ModuleCommandEvent;
	import com.bizfx.utils.StringUtils;
	
	import mx.events.FlexEvent;
	import mx.logging.ILogger;
	import mx.logging.Log;
	
	import org.springextensions.actionscript.module.BasicSASModule;
	import org.springextensions.actionscript.stage.DefaultAutowiringStageProcessor;
	
	public class ViewModule extends BasicSASModule implements IModule
	{
		private var logger:ILogger;
		
		private var _moduleContextPath:String;
		/**
		 * 模块context路径。默认值为"configs/[module名称]-context.xml"。
		 */
		public function get moduleContextPath():String
		{
			return _moduleContextPath;
		}
		
		public function set moduleContextPath(value:String):void
		{
			_moduleContextPath = value;
		}
		
		private var _moduleViewContextName:String;
		/**
		 * 模块视图名称。默认值为类名首写字母小写+View。
		 */
		public function get moduleViewContextName():String
		{
			return _moduleViewContextName;
		}
		
		public function set moduleViewContextName(value:String):void
		{
			_moduleViewContextName = value;
		}
		
		private var _moduleName:String;
		
		public function get moduleName():String
		{
			return _moduleName;
		}
		
		public function set moduleName(value:String):void
		{
			_moduleName = value;
		}
		
		private var _moduleIcon:Class;
		
		public function get moduleIcon():Class
		{
			return _moduleIcon;
		}
		
		public function set moduleIcon(value:Class):void
		{
			_moduleIcon = value;
		}
		
		private var _view:IView;
		
		public function get view():IView
		{
			return _view;
		}
		
		private var _mainForm:IMainForm;

		public function get mainForm():IMainForm
		{
			return _mainForm;
		}

		public function set mainForm(value:IMainForm):void
		{
			_mainForm = value;
			if (value) {
				this._mainForm.addEventListener(ModuleCommandEvent.ModuleCommand_Handle, moduleCommandHandleHandler);
			}
		}
		
		public function ViewModule()
		{
			if (StringUtils.isNullOrEmpty(this.moduleContextPath)) {
				this.moduleContextPath = "configs/" + this.className + "-context.xml";
			}
			logger = Log.getLogger(this.className);
			super();
			this.addEventListener(FlexEvent.CREATION_COMPLETE, creationCompleteHandler);
		}
				
		public function refresh():void {
			if (this.view) {
				view.refresh();				
			}
		}
		/**
		 * 子类通过重载次函数对模块里所用到的类进行初始化。
		 */
		protected function initClasses():void {
		}
		
		private function moduleCommandRaiseHandler(event:ModuleCommandEvent):void {
			this.dispatchEvent(new ModuleCommandEvent(ModuleCommandEvent.ModuleCommand_Raise, event.commandType));
		}
		
		private function moduleCommandHandleHandler(event:ModuleCommandEvent):void {
			this.dispatchEvent(new ModuleCommandEvent(ModuleCommandEvent.ModuleCommand_Handle, event.commandType));
		}
		
		private function creationCompleteHandler(event:FlexEvent):void
		{
			this.initClasses();
			var ap:DefaultAutowiringStageProcessor;
			moduleApplicationContext.addConfigLocation(moduleContextPath);
			moduleApplicationContext.addEventListener(Event.COMPLETE, contextLoadCompletedHandler);
			moduleApplicationContext.addEventListener(IOErrorEvent.DISK_ERROR, contextLoadErrorHandler);
			logger.debug("准备读取spring的" + moduleContextPath + "配置文件。");
			moduleApplicationContext.load();
		}
		
		private function contextLoadCompletedHandler(e:Event):void {
			logger.debug("完成读取spring的" + moduleContextPath + "配置文件。");
			moduleApplicationContext.removeEventListener(Event.COMPLETE, contextLoadCompletedHandler);
			if (StringUtils.isNullOrEmpty(moduleViewContextName))
			{
				moduleViewContextName = className.substr(0, 1).toLowerCase() + className.substr(1) + "View";
			}
			if (moduleApplicationContext.containsObject(moduleViewContextName))
			{
				var moduleView:IView = moduleApplicationContext.getObject(moduleViewContextName) as IView;
				moduleView.addEventListener(ModuleCommandEvent.ModuleCommand_Raise, moduleCommandRaiseHandler);
				moduleView.module = this;
				this._view = moduleView;
				this.addChild(moduleView as DisplayObject);
			}
			else
			{
				logger.debug("获取不到【" + moduleViewContextName + "】ModuleView");
			}
		}
		
		private function contextLoadErrorHandler(e:IOErrorEvent):void {
			logger.error("读取spring的" + moduleContextPath + "配置文件发生异常：" + e.text); 
		}
	}
}